import math
import math
from re import I
from turtle import *
from utils import relative_move
import random
class sky:
    def __init__(self) -> None:
        self.__color1__=(0, 71, 137)  #渐变的两种颜色
        self.__color2__=(250, 102, 62)
        self.__ox__=0
        self.__oy__=0
    def setcolor(self,color1,color2):
        __color1__=color1
        __color2__=color2
    def cloud(self,size):
        seth(0)
        color((254, 253, 223),(254, 253, 223))
        begin_fill()
        fd(80*size)
        circle(30*size,180)
        seth(90)
        circle(40*size,180)
        seth(180)
        circle(30*size,180)
        end_fill()
    def gradualcolor(self,color1,color2,i,n) -> tuple:   #n可理解为渐变精度,i为1-n的数,返回rgb元组 
        r=int(color1[0]+i*((color2[0]-color1[0])/n))
        g=int(color1[1]+i*((color2[1]-color1[1])/n))
        b=int(color1[2]+i*((color2[2]-color1[2])/n))
        return (r,g,b)
    def background(self):
        getscreen().colormode(255)
        begin_poly()
        fd(640)
        right(90)
        fd(1)
        right(90)
        fd(640)
        right(90)
        fd(1)
        end_poly()
        skyblock = get_poly() 
        register_shape("skyblock", skyblock)
        shape("skyblock")
        for i in range(200):
            relative_move(0,-2)
            pu()
            pencolor(self.gradualcolor(self.__color1__,self.__color2__,i,200))
            stamp()
    def run(self):
        getscreen().colormode(255)
        seth(0)
        s=position()
        self.background()
        shape("turtle")
        s=(s[0]*2,s[1]*2)
        pencolor((15, 17, 26))
        for i in range(12):
            pu()
            goto(s)
            pd()
            x=random.random()*740-60
            y=-random.random()*200-100
            pu()
            relative_move(x,y)
            self.cloud(random.random()*0.5+0.2)
        for i in range(8):
            pu()
            goto(s)
            pd()
            x=random.random()*740-60
            y=-random.random()*200
            pu()
            relative_move(x,y)
            self.cloud(random.random()*0.5+0.2)
    def __sun__(self):
        color("orange","orange")
        for i in range(180):
            y=math.sin(math.radians(i))*340
            x=math.cos(math.radians(i))*340
            goto(self.__ox__-x,self.__oy__+y)
            shape("sunshape") 
    def __luna__(self):
        color("yellow","yellow")
        for i in range(180):
            y=math.sin(math.radians(i))*340
            x=math.cos(math.radians(i))*340
            goto(self.__ox__-x,self.__oy__+y)
            shape("lunashape")
    def setXY(self,ox,oy):
        self.__ox__=ox
        self.__oy__=oy
    def move(self,ox,oy):    #sun and moon 的动画效果
        begin_poly()
        begin_fill()
        pu()
        circle(20,360)
        end_fill()
        end_poly()
        undo()
        sunshape = get_poly() 
        register_shape("sunshape", sunshape)
        begin_poly()
        begin_fill()
        circle(20,180)
        left(130)
        circle(-30,75)
        end_fill()
        end_poly()
        undo()
        lunashape = get_poly() 
        register_shape("lunashape", lunashape)
        self.setXY(ox,oy)
        while True:
            self.__sun__()
            self.__luna__()
